BottomNavigationView底部導航欄,是一個常見的元件,常用於Fragment畫面的切換。
implementation 'com.google.android.material:material:1.3.0-alpha01'
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
其中三張為底部導航欄圖片,另三張為點擊時,上方imageView切換的圖片。
於item中設定對應的title、id與icon。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="首頁"
android:id="@+id/na_home"
android:icon="@drawable/ic_baseline_home">
</item>
<item android:title="設定"
android:id="@+id/na_setting"
android:icon="@drawable/ic_baseline_settings">
</item>
<item android:title="設定"
android:id="@+id/na_other"
android:icon="@drawable/ic_baseline_other">
</item>
</menu>
這邊設定的id在後續點擊事件中會使用到。
於activity_main.xml中綁定
app:menu="@menu/menu"
app:labelVisibilityMode="selected"
這邊提供四種參數供使用:
於drawable加入sl_color.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:color="@color/purple_200"/>
<item android:state_checked="true" android:color="@color/teal_200"/>
</selector>
於activity_main.xml中綁定
app:itemIconTint="@drawable/sl_color"
app:itemTextColor="@drawable/sl_color"
app:itemRippleColor="@color/teal_200"
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.na_home:
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_baseline_android));
break;
case R.id.na_setting:
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_baseline_accessibility));
break;
case R.id.na_other:
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_baseline_account_circle));
break;
}
return true;
}
});
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="selected"
app:menu="@menu/menu"
app:itemIconTint="@drawable/sl_color"
app:itemTextColor="@drawable/sl_color"
app:itemRippleColor="@color/teal_200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</com.google.android.material.bottomnavigation.BottomNavigationView>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_baseline_android" />
</androidx.constraintlayout.widget.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.navigation);
imageView = findViewById(R.id.imageView);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.na_home:
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_baseline_android));
break;
case R.id.na_setting:
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_baseline_accessibility));
break;
case R.id.na_other:
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_baseline_account_circle));
break;
}
return true;
}
});
}
}